#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


char* entityParser(char* text) {
    int n = strlen(text);
    char* ans = (char*)malloc(sizeof(char) * (n + 1));
    int i = 0;
    int pos = 0;

    int start = 0;
    int end = 0;
    int j = 0;
    while (i < n)
    {
        if (text[i] != '&')
        {
            ans[j++] = text[i++];
        }
        else
        {
            start = i;
            int k = i + 1;
            if (k == n)
                goto end;
            while (k < n)
            {
                if (text[k] == ';')
                {
                    end = k;
                    break;
                }
                else if (text[k] == '&')
                {
                    end = k - 1;
                    break;
                }
                else
                    k++;
            }
            int len = end - start + 1;
            if (len == 6 && strncmp(text + start, "&quot;", len) == 0)
            {
                ans[j++] = '\"';
            }
            else if (len == 6 && strncmp(text + start, "&apos;", len) == 0)
                ans[j++] = '\'';
            else if (len == 5 && strncmp(text + start, "&amp;", len) == 0)
                ans[j++] = '&';
            else if (len == 4 && strncmp(text + start, "&gt;", len) == 0)
                ans[j++] = '>';
            else if (len == 4 && strncmp(text + start, "&lt;", len) == 0)
                ans[j++] = '<';
            else if (len == 7 && strncmp(text + start, "&frasl;", len) == 0)
                ans[j++] = '/';
            else
            {
                strncpy(ans + j, text + start, sizeof(char) * len);
                j += len;
            }
            i += len;

        }
    }
end:
    while (i < n)
    {
        ans[j++] = text[i++];
    }
    ans[j] = '\0';
    return ans;
}

int main()
{
    char s[] = { "&&& &amp; is a &" };
    char* ret = entityParser(s);
    printf("%s\n", ret);
    return 0;
}
